package com.audi.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.audi.R;
import com.audi.BaseActivity;
import com.audi.adapter.SmartTriggerAdapter;
import com.audi.dto.DescriptionSmartTrigger;
import com.audi.dto.DeviceItem;
import com.audi.dto.SmartTriggerWhenItem;
import com.audi.view.BaseDeviceDialog;
import com.audi.view.DeviceUpdatePhoto;

public class InfoDeviceActivity extends BaseActivity {
	private Button mBtnInfo, mBtnSmartTrigger;
	private LinearLayout mLinearLayoutSmartTrigger, mLinearLayoutInfoDevice;
	private ImageButton mImageButtonOnOff;
	private ListView mLstSmartTrigger;
	private ArrayList<SmartTriggerWhenItem> mArrTriggerInfoDeviceActivity = new ArrayList<SmartTriggerWhenItem>();
	private SmartTriggerAdapter adapterSmartTrigger = null;
	private ImageButton mIbtnDevicesCardMenu, mIbtnDevicesBack;
	private TextView mTxtDeviceName;
	private DeviceUpdatePhoto dialogUpdatePhoto;
	private DeviceItem itemDeviceInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_device_main);
		initActionBar(R.layout.action_bar_device_card_detail);
		setDisableDrawer();
		mBtnInfo = (Button) findViewById(R.id.btn_info_device);
		mBtnSmartTrigger = (Button) findViewById(R.id.btn_smart_trigger);
		mLinearLayoutInfoDevice = (LinearLayout) findViewById(R.id.linear_layout_info_device);
		mLinearLayoutSmartTrigger = (LinearLayout) findViewById(R.id.linear_layout_smart_trigger);
		mImageButtonOnOff = (ImageButton) findViewById(R.id.ibtn_control);
		mLstSmartTrigger = (ListView) findViewById(R.id.lst_smart_trigger);
		mIbtnDevicesCardMenu = (ImageButton) findViewById(R.id.btn_menu_devices_card);
		mIbtnDevicesBack = (ImageButton) findViewById(R.id.btn_back_devices_name);
		mTxtDeviceName = (TextView) findViewById(R.id.txt_devices_name);
		dialogUpdatePhoto = new DeviceUpdatePhoto(this);
		itemDeviceInfo = (DeviceItem) getIntent().getExtras().getSerializable(
				"itemDeviceCard");
		/*Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("DATA_DEVICE");
		itemDeviceInfo = (DeviceItem) bundle.getSerializable("itemDeviceCard");*/
		mTxtDeviceName.setText(itemDeviceInfo.getDeviceName().toString());
		mIbtnDevicesBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(InfoDeviceActivity.this, "Back",
						Toast.LENGTH_SHORT).show();
				finish();
			}
		});
		mIbtnDevicesCardMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				PopupMenu popupMenu = new PopupMenu(InfoDeviceActivity.this,
						mIbtnDevicesCardMenu);
				popupMenu.getMenuInflater().inflate(
						R.menu.popup_menu_sub_device_card_valid,
						popupMenu.getMenu());
				popupMenu
						.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
							public boolean onMenuItemClick(MenuItem item) {
								if (item.getItemId() == R.id.item_update_photo) {
									dialogUpdatePhoto.updatePhoto();
								}
								if (item.getItemId() == R.id.item_rename) {
									BaseDeviceDialog renameDevice = new BaseDeviceDialog(
											InfoDeviceActivity.this);
									renameDevice.editNameDevice("1");
								}
								if (item.getItemId() == R.id.item_change_place) {
									BaseDeviceDialog changePlaceDialog = new BaseDeviceDialog(
											InfoDeviceActivity.this);
									changePlaceDialog.showSelectPlaceDialog();
								}
								if (item.getItemId() == R.id.item_remove_device) {
									BaseDeviceDialog removeDeviceDialog = new BaseDeviceDialog(
											InfoDeviceActivity.this);
									// removeDeviceDialog.removeDevice(new
									// DeviceItem(),handler);
								}
								return true;
							}
						});
				popupMenu.show();

			}
		});

		setOnClickButtonForInfo();
		setOnClickButtonForSmartTrigger();
		dumpDataInfoDeviceActivity();

//		adapterSmartTrigger = new SmartTriggerAdapter(this,
//				mArrTriggerInfoDeviceActivity);
//		mLstSmartTrigger.setAdapter(adapterSmartTrigger);

	}

	private void setOnClickButtonForInfo() {
		mBtnInfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mBtnInfo.setBackgroundResource(R.drawable.button_pressed);
				mBtnSmartTrigger
						.setBackgroundResource(R.drawable.button_normal);
				mLinearLayoutInfoDevice.setVisibility(View.VISIBLE);
				mLinearLayoutSmartTrigger.setVisibility(View.GONE);
				mImageButtonOnOff.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Toast.makeText(InfoDeviceActivity.this,
								"On or Off Device", Toast.LENGTH_SHORT).show();

					}
				});

			}
		});
	}

	private void dumpDataInfoDeviceActivity() {
//		mArrTriggerInfoDeviceActivity.add(new DescriptionSmartTrigger("", "",
//				"", "", "", "", 1, 1));
//		mArrTriggerInfoDeviceActivity.add(new DescriptionSmartTrigger("", "",
//				"", "", "", "", 0, 0));
		// mArrTriggerInfoDeviceActivity.add(new DescriptionSmartTrigger("", "",
		// "", "", "", "", 0,
		// 0));
		// mArrTriggerInfoDeviceActivity.add(new DescriptionSmartTrigger("", "",
		// "", "", "", "", 0,
		// 0));
		// mArrTriggerInfoDeviceActivity.add(new DescriptionSmartTrigger("", "",
		// "", "", "", "", 0,
		// 0));
	}

	private void setOnClickButtonForSmartTrigger() {
		mBtnSmartTrigger.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mBtnInfo.setBackgroundResource(R.drawable.button_normal);
				mBtnSmartTrigger
						.setBackgroundResource(R.drawable.button_pressed);
				mLinearLayoutInfoDevice.setVisibility(View.GONE);
				mLinearLayoutSmartTrigger.setVisibility(View.VISIBLE);

				mLstSmartTrigger
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								arg1.setEnabled(false);
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								// TODO Auto-generated method stub

							}
						});

			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == DeviceUpdatePhoto.cameraCapture) {
				dialogUpdatePhoto.performcrop(dialogUpdatePhoto.getPicUri());
			} else if (requestCode == DeviceUpdatePhoto.picCrop) {
				Toast.makeText(getApplicationContext(), "DONE",
						Toast.LENGTH_SHORT).show();
			}
			if (requestCode == DeviceUpdatePhoto.pickFromFile) {
				dialogUpdatePhoto.performcrop(data.getData());
			}
		}
	}

	@Override
	public void onRefreshData() {
		// TODO Auto-generated method stub

	}
}